<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目id" prop="projectId">
        <el-input
          v-model="queryParams.projectId"
          placeholder="请输入项目id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.flaw_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="故事点数" prop="storyPoints">
        <el-input
          v-model="queryParams.storyPoints"
          placeholder="请输入故事点数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="优先级" prop="priority">
        <el-input
          v-model="queryParams.priority"
          placeholder="请输入优先级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结果" prop="outcome">
        <el-select v-model="queryParams.outcome" placeholder="请选择结果" clearable>
          <el-option
            v-for="dict in dict.type.test_outcome"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="测试计划ID" prop="testPlanId">
        <el-input
          v-model="queryParams.testPlanId"
          placeholder="请输入测试计划ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="测试ID" prop="testId">
        <el-input
          v-model="queryParams.testId"
          placeholder="请输入测试ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工作项ID" prop="workItemId">
        <el-input
          v-model="queryParams.workItemId"
          placeholder="请输入工作项ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人ID" prop="responsibleId">
        <el-input
          v-model="queryParams.responsibleId"
          placeholder="请输入负责人ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="严重程度" prop="severity">
        <el-select v-model="queryParams.severity" placeholder="请选择严重程度" clearable>
          <el-option
            v-for="dict in dict.type.order_of_severity"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="缺陷类型" prop="defectClass">
        <el-select v-model="queryParams.defectClass" placeholder="请选择缺陷类型" clearable>
          <el-option
            v-for="dict in dict.type.defect_class"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="重现概率" prop="recurrenceProbability">
        <el-select v-model="queryParams.recurrenceProbability" placeholder="请选择重现概率" clearable>
          <el-option
            v-for="dict in dict.type.recurrence_probability"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="原因分析" prop="causeAnalysis">
        <el-select v-model="queryParams.causeAnalysis" placeholder="请选择原因分析" clearable>
          <el-option
            v-for="dict in dict.type.cause_analysis"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="解决方案" prop="solution">
        <el-select v-model="queryParams.solution" placeholder="请选择解决方案" clearable>
          <el-option
            v-for="dict in dict.type.solution"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="预估工时" prop="estimatedWorkingHours">
        <el-input
          v-model="queryParams.estimatedWorkingHours"
          placeholder="请输入预估工时"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="登记工时" prop="registerWorkingHours">
        <el-input
          v-model="queryParams.registerWorkingHours"
          placeholder="请输入登记工时"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="剩余工时" prop="surplusWorkingHours">
        <el-input
          v-model="queryParams.surplusWorkingHours"
          placeholder="请输入剩余工时"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdAt">
        <el-date-picker clearable
                        v-model="queryParams.createdAt"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="创建人ID" prop="createdBy">
        <el-input
          v-model="queryParams.createdBy"
          placeholder="请输入创建人ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新时间" prop="updatedAt">
        <el-date-picker clearable
                        v-model="queryParams.updatedAt"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新人ID" prop="updatedBy">
        <el-input
          v-model="queryParams.updatedBy"
          placeholder="请输入更新人ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['project:flaw:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['project:flaw:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['project:flaw:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['project:flaw:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="flawList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="flawId" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="项目id" align="center" prop="projectId" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.flaw_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="故事点数" align="center" prop="storyPoints" />
      <el-table-column label="优先级" align="center" prop="priority">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.flaw_priority" :value="scope.row.priority"/>
        </template>
      </el-table-column>
      <el-table-column label="结果" align="center" prop="outcome">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.test_outcome" :value="scope.row.outcome"/>
        </template>
      </el-table-column>
      <el-table-column label="测试计划ID" align="center" prop="testPlanId" />
      <el-table-column label="测试ID" align="center" prop="testId" />
      <el-table-column label="工作项ID" align="center" prop="workItemId" />
      <el-table-column label="负责人ID" align="center" prop="responsibleId" />
      <el-table-column label="严重程度" align="center" prop="severity">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_of_severity" :value="scope.row.severity"/>
        </template>
      </el-table-column>
      <el-table-column label="缺陷类型" align="center" prop="defectClass">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.defect_class" :value="scope.row.defectClass"/>
        </template>
      </el-table-column>
      <el-table-column label="重现概率" align="center" prop="recurrenceProbability">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.recurrence_probability" :value="scope.row.recurrenceProbability"/>
        </template>
      </el-table-column>
      <el-table-column label="原因分析" align="center" prop="causeAnalysis">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cause_analysis" :value="scope.row.causeAnalysis"/>
        </template>
      </el-table-column>
      <el-table-column label="解决方案" align="center" prop="solution">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.solution" :value="scope.row.solution"/>
        </template>
      </el-table-column>
      <el-table-column label="重现步骤" align="center" prop="reproductionStep" />
      <el-table-column label="预估工时" align="center" prop="estimatedWorkingHours" />
      <el-table-column label="登记工时" align="center" prop="registerWorkingHours" />
      <el-table-column label="剩余工时" align="center" prop="surplusWorkingHours" />
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人ID" align="center" prop="createdBy" />
      <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人ID" align="center" prop="updatedBy" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['project:flaw:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['project:flaw:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改缺陷对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="描述">
          <editor v-model="form.description" :min-height="192"/>
        </el-form-item>
        <el-form-item label="项目id" prop="projectId">
          <el-input v-model="form.projectId" placeholder="请输入项目id" />
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.flaw_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="故事点数" prop="storyPoints">
          <el-input v-model="form.storyPoints" placeholder="请输入故事点数" />
        </el-form-item>
        <el-form-item label="优先级" prop="priority">
          <el-input v-model="form.priority" placeholder="请输入优先级" />
        </el-form-item>
        <el-form-item label="结果" prop="outcome">
          <el-select v-model="form.outcome" placeholder="请选择结果">
            <el-option
              v-for="dict in dict.type.test_outcome"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="测试计划ID" prop="testPlanId">
          <el-input v-model="form.testPlanId" placeholder="请输入测试计划ID" />
        </el-form-item>
        <el-form-item label="测试ID" prop="testId">
          <el-input v-model="form.testId" placeholder="请输入测试ID" />
        </el-form-item>
        <el-form-item label="工作项ID" prop="workItemId">
          <el-input v-model="form.workItemId" placeholder="请输入工作项ID" />
        </el-form-item>
        <el-form-item label="负责人ID" prop="responsibleId">
          <el-input v-model="form.responsibleId" placeholder="请输入负责人ID" />
        </el-form-item>
        <el-form-item label="严重程度" prop="severity">
          <el-select v-model="form.severity" placeholder="请选择严重程度">
            <el-option
              v-for="dict in dict.type.order_of_severity"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="缺陷类型" prop="defectClass">
          <el-select v-model="form.defectClass" placeholder="请选择缺陷类型">
            <el-option
              v-for="dict in dict.type.defect_class"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="重现概率" prop="recurrenceProbability">
          <el-select v-model="form.recurrenceProbability" placeholder="请选择重现概率">
            <el-option
              v-for="dict in dict.type.recurrence_probability"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="原因分析" prop="causeAnalysis">
          <el-select v-model="form.causeAnalysis" placeholder="请选择原因分析">
            <el-option
              v-for="dict in dict.type.cause_analysis"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="解决方案" prop="solution">
          <el-select v-model="form.solution" placeholder="请选择解决方案">
            <el-option
              v-for="dict in dict.type.solution"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="重现步骤" prop="reproductionStep">
          <el-input v-model="form.reproductionStep" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="预估工时" prop="estimatedWorkingHours">
          <el-input v-model="form.estimatedWorkingHours" placeholder="请输入预估工时" />
        </el-form-item>
        <el-form-item label="登记工时" prop="registerWorkingHours">
          <el-input v-model="form.registerWorkingHours" placeholder="请输入登记工时" />
        </el-form-item>
        <el-form-item label="剩余工时" prop="surplusWorkingHours">
          <el-input v-model="form.surplusWorkingHours" placeholder="请输入剩余工时" />
        </el-form-item>
        <el-form-item label="创建人ID" prop="createdBy">
          <el-input v-model="form.createdBy" placeholder="请输入创建人ID" />
        </el-form-item>
        <el-form-item label="更新人ID" prop="updatedBy">
          <el-input v-model="form.updatedBy" placeholder="请输入更新人ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFlaw, getFlaw, delFlaw, addFlaw, updateFlaw } from "@/api/projectAdmin/flaw";

export default {
  name: "Flaw",
  dicts: ['defect_class', 'flaw_status', 'cause_analysis', 'test_outcome', 'solution', 'order_of_severity', 'recurrence_probability'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 缺陷表格数据
      flawList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        description: null,
        projectId: null,
        projectName: null,
        status: null,
        storyPoints: null,
        priority: null,
        outcome: null,
        testPlanId: null,
        testId: null,
        workItemId: null,
        responsibleId: null,
        severity: null,
        defectClass: null,
        recurrenceProbability: null,
        causeAnalysis: null,
        solution: null,
        reproductionStep: null,
        estimatedWorkingHours: null,
        registerWorkingHours: null,
        surplusWorkingHours: null,
        createdAt: null,
        createdBy: null,
        updatedAt: null,
        updatedBy: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        testPlanId: [
          { required: true, message: "测试计划ID不能为空", trigger: "blur" }
        ],
        testId: [
          { required: true, message: "测试ID不能为空", trigger: "blur" }
        ],
        workItemId: [
          { required: true, message: "工作项ID不能为空", trigger: "blur" }
        ],
        createdAt: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        createdBy: [
          { required: true, message: "创建人ID不能为空", trigger: "blur" }
        ],
        updatedAt: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ],
        updatedBy: [
          { required: true, message: "更新人ID不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询缺陷列表 */
    getList() {
      this.loading = true;
      listFlaw(this.queryParams).then(response => {
        this.flawList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        flawId: null,
        title: null,
        description: null,
        projectId: null,
        projectName: null,
        status: null,
        storyPoints: null,
        priority: null,
        outcome: null,
        testPlanId: null,
        testId: null,
        workItemId: null,
        responsibleId: null,
        severity: null,
        defectClass: null,
        recurrenceProbability: null,
        causeAnalysis: null,
        solution: null,
        reproductionStep: null,
        estimatedWorkingHours: null,
        registerWorkingHours: null,
        surplusWorkingHours: null,
        createdAt: null,
        createdBy: null,
        updatedAt: null,
        updatedBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.flawId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加缺陷";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const flawId = row.flawId || this.ids
      getFlaw(flawId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改缺陷";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.flawId != null) {
            updateFlaw(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFlaw(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const flawIds = row.flawId || this.ids;
      this.$modal.confirm('是否确认删除缺陷编号为"' + flawIds + '"的数据项？').then(function() {
        return delFlaw(flawIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('project/flaw/export', {
        ...this.queryParams
      }, `flaw_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
